package com.jindy.module.engineeringcost.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.constant.HttpStatus;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.engineeringcost.domain.EngineeringCost;
import com.jindy.module.engineeringcost.domain.EngineeringCostApprovalResult;
import com.jindy.module.engineeringcost.service.IEngineeringCostService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【工程部费用】Controller
 *
 * @author jindy
 * @date 2023-01-31
 */
@RestController
@RequestMapping("/module/cost")
public class EngineeringCostController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(EngineeringCostController.class);
    @Autowired
    private IEngineeringCostService engineeringCostService;

    /**
     * 查询【工程部费用】列表
     */
    @PreAuthorize("@ss.hasPermi('module:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(EngineeringCost engineeringCost) {
        try {
            startPage();
            List<EngineeringCostApprovalResult> list = engineeringCostService.selectEngineeringCostListPermission(engineeringCost);
            return getDataTable(list);
        } catch (Exception e) {
            LOGGER.error("查询【工程部费用】列表失败：",e);
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg(e.getMessage());
            return rspData;
        }
    }

    /**
     * 导出【工程部费用】列表
     */
    @PreAuthorize("@ss.hasPermi('module:cost:export')")
    @Log(title = "【工程部费用】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EngineeringCost engineeringCost) {
        List<EngineeringCost> list = engineeringCostService.selectEngineeringCostList(engineeringCost);
        ExcelUtil<EngineeringCost> util = new ExcelUtil<EngineeringCost>(EngineeringCost.class);
        util.exportExcel(response, list, "【工程部费用】数据");
    }

    /**
     * 获取【工程部费用】详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:cost:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(engineeringCostService.selectEngineeringCostById(id));
    }

    /**
     * 新增【工程部费用】
     */
    @PreAuthorize("@ss.hasPermi('module:cost:add')")
    @Log(title = "【工程部费用】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EngineeringCost engineeringCost) {
        try {
            return engineeringCostService.insertEngineeringCost(engineeringCost);
        } catch (Exception e) {
           LOGGER.error("新增【工程部费用】失败",e);
           return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改【工程部费用】
     */
    @PreAuthorize("@ss.hasPermi('module:cost:edit')")
    @Log(title = "【工程部费用】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EngineeringCost engineeringCost) {
        return toAjax(engineeringCostService.updateEngineeringCost(engineeringCost));
    }

    /**
     * 删除【工程部费用】
     */
    @PreAuthorize("@ss.hasPermi('module:cost:remove')")
    @Log(title = "【工程部费用】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(engineeringCostService.deleteEngineeringCostByIds(ids));
    }
}
